package com.pcafs.manage.controller;

import com.pcafs.common.annotation.Log;
import com.pcafs.common.core.controller.BaseController;
import com.pcafs.common.core.domain.AjaxResult;
import com.pcafs.common.core.domain.entity.PcafsDatabase;
import com.pcafs.common.core.page.TableDataInfo;
import com.pcafs.common.enums.BusinessType;
import com.pcafs.common.utils.poi.ExcelUtil;
import com.pcafs.manage.bo.PcafsDatabaseAddBo;
import com.pcafs.manage.bo.PcafsDatabaseEditBo;
import com.pcafs.manage.bo.PcafsDatabaseQueryBo;
import com.pcafs.manage.service.IPcafsDatabaseService;
import com.pcafs.manage.vo.PcafsDatabaseVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 财务数据仓Controller
 * 
 * @author pcafs
 * @date 2022-05-02
 */
@Api(value = "财务数据仓控制器", tags = {"财务数据仓管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/manage/database")
public class PcafsDatabaseController extends BaseController {

    private final IPcafsDatabaseService iPcafsDatabaseService;

    /**
     * 查询财务数据仓列表
     */
    @ApiOperation("查询财务数据仓列表")
//    @PreAuthorize("@ss.hasPermi('manage:database:list')")
    @GetMapping("/list")
    public TableDataInfo<PcafsDatabaseVo> list(PcafsDatabaseQueryBo bo) {
        startPage();
        List<PcafsDatabaseVo> list = iPcafsDatabaseService.queryList(bo);
        return getDataTable(list);
    }

    /**
     * 导出财务数据仓列表
     */
    @ApiOperation("导出财务数据仓列表")
//    @PreAuthorize("@ss.hasPermi('manage:database:export')")
    @Log(title = "财务数据仓", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<PcafsDatabaseVo> export(PcafsDatabaseQueryBo bo) {
        List<PcafsDatabaseVo> list = iPcafsDatabaseService.queryList(bo);
        ExcelUtil<PcafsDatabaseVo> util = new ExcelUtil<PcafsDatabaseVo>(PcafsDatabaseVo.class);
        return util.exportExcel(list, "财务数据仓");
    }

    /**
     * 获取财务数据仓详细信息
     */
    @ApiOperation("获取财务数据仓详细信息")
//    @PreAuthorize("@ss.hasPermi('manage:database:query')")
    @GetMapping("/{id}")
    public AjaxResult<PcafsDatabaseVo> getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iPcafsDatabaseService.queryById(id));
    }

    /**
     * 新增财务数据仓
     */
    @ApiOperation("新增财务数据仓")
//    @PreAuthorize("@ss.hasPermi('manage:database:add')")
    @Log(title = "财务数据仓", businessType = BusinessType.INSERT)
    @PostMapping()
    public AjaxResult<Boolean> add(@RequestBody PcafsDatabaseAddBo bo) {
        return AjaxResult.success(iPcafsDatabaseService.insertByAddBo(bo));
    }

    /**
     * 新增父级
     */
    @ApiOperation("新增父级")
    @Log(title = "新增父级", businessType = BusinessType.INSERT)
    @PostMapping("/addParent")
    public AjaxResult<Void> addParent(@RequestBody PcafsDatabaseAddBo bo) {
        return toAjax(iPcafsDatabaseService.addParent(bo) ? 1 : 0);
    }


    /**
     * 修改财务数据仓
     */
    @ApiOperation("修改财务数据仓")
//    @PreAuthorize("@ss.hasPermi('manage:database:edit')")
    @Log(title = "财务数据仓", businessType = BusinessType.UPDATE)
    @PutMapping()
    public AjaxResult<Void> edit(@RequestBody PcafsDatabaseEditBo bo) {
        return toAjax(iPcafsDatabaseService.updateByEditBo(bo) ? 1 : 0);
    }

    /**
     * 获取下拉树列表
     */
    @GetMapping("/getTree")
    public AjaxResult getTree() {
        List<PcafsDatabase> list = iPcafsDatabaseService.list();
        return AjaxResult.success(iPcafsDatabaseService.buildTree(list));
    }

    /**
     * 删除财务数据仓
     */
    @ApiOperation("删除财务数据仓")
//    @PreAuthorize("@ss.hasPermi('manage:database:remove')")
    @Log(title = "财务数据仓" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@PathVariable Long[] ids) {
        return toAjax(iPcafsDatabaseService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }
}
